package com.sphd.restaurantsystem.activitys;

import android.app.Activity;
import android.bluetooth.BluetoothAdapter;
import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.Toolbar;
import android.view.KeyEvent;
import android.widget.ImageView;
import android.widget.Toast;

import com.sphd.restaurantsystem.R;
import com.sphd.restaurantsystem.adapters.MyViewPagerAdapter;
import com.sphd.restaurantsystem.base.BaseActivity;
import com.sphd.restaurantsystem.fragments.rec.StateFragment;
import com.sphd.restaurantsystem.info.RestaurantInfo;
import com.sphd.restaurantsystem.interfaces.SatateValCallBack;
import com.sphd.restaurantsystem.utils.Constant;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 接单员操作界面
 */
public class AllOrdersActivity extends BaseActivity implements SatateValCallBack {

    @BindView(R.id.toolbar)
    Toolbar toolbar;
    @BindView(R.id.tabs)
    TabLayout tabs;
    @BindView(R.id.my_vp)
    ViewPager myVp;
    List<Fragment> fragmentList;
    MyViewPagerAdapter myViewPagerAdapter;
    String titles[] = {"", "", "", "", ""};
    Intent intent;
    Bundle bundle;
    RestaurantInfo restaurantInfo;
    @BindView(R.id.set_bt)
    ImageView setBt;
    private static final int REFRESH_COMPLETE = 0X110;
    private static final int ENABLE = 0X111;
    public static final int REQUEST_ENABLE_BT = 2;
    private BluetoothAdapter mBluetoothAdapter;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_all_orders);
        ButterKnife.bind(this);
        initView();
        initEvent();
    }

    private void initEvent() {
        toolbar.setNavigationOnClickListener(view -> {
            setResult(1);
            finish();
        });

    }
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if(keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_DOWN){
            setResult(1);
            finish();
        }
        return super.onKeyDown(keyCode, event);
    }
    private void initView() {
        intent = getIntent();
        bundle = intent.getExtras();
        restaurantInfo = (RestaurantInfo) bundle.getSerializable(Constant.RESTAURANTSINFO);
        mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
        setTitleBar(toolbar, "实时订单");
        fragmentList = new ArrayList<>();
        for (int i = 0; i < 5; i++) {
            StateFragment stateFragment = new StateFragment();
            if (i == 4) {
                stateFragment.setCallBack(this, restaurantInfo, 6);
            } else {
                stateFragment.setCallBack(this, restaurantInfo, i);
            }
            fragmentList.add(stateFragment);
        }

        myViewPagerAdapter = new MyViewPagerAdapter(getSupportFragmentManager(), fragmentList, titles);
        myVp.setAdapter(myViewPagerAdapter);
        tabs.setupWithViewPager(myVp);

    }

    @Override
    public void returnVal(int status0sum, int status1sum, int status2sum, int status3sum, int status6sum) {
        titles[0] = String.valueOf(status0sum);
        titles[1] = String.valueOf(status1sum);
        titles[2] = String.valueOf(status2sum);
        titles[3] = String.valueOf(status3sum);
        titles[4] = String.valueOf(status6sum);
        myViewPagerAdapter.notifyDataSetChanged();
    }

    @Override
    public void setCurrentItem(int pos) {
        myVp.setCurrentItem(pos);
    }

    @OnClick(R.id.set_bt)
    public void onClick() {
        getBluetoothDevice();
    }

    private void getBluetoothDevice() {
        if (mBluetoothAdapter == null) {
            toast("不支持蓝牙的设备");
        } else {
            // If BT is not on, request that it be enabled.
            // setupChat() will then be called during onActivityResult
            if (!mBluetoothAdapter.isEnabled()) {
                intent = new Intent(
                        BluetoothAdapter.ACTION_REQUEST_ENABLE);
                startActivityForResult(intent, REQUEST_ENABLE_BT);
            } else {
                //添加打印机
                intent.setClass(context, AddPrinterActivity.class);
                startActivity(intent);
            }
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == REQUEST_ENABLE_BT) {
            if (resultCode == Activity.RESULT_OK) {//如果打开蓝牙就跳抓到打印机设置界面
                // bluetooth is opened
                // select bluetooth device fome list
                intent.setClass(context, AddPrinterActivity.class);

                startActivity(intent);
            } else {
                // bluetooth is not open
                Toast.makeText(this, R.string.bluetooth_is_not_enabled,
                        Toast.LENGTH_SHORT).show();
            }
        }
    }


}
